<?php
namespace Admin\Controller;
use Think\Controller;
class LoginController extends Controller {
    public function __construct()
    {
        parent::__construct();
    }

    public function index(){
        $this->display('login');
    }

    //验证码
    public function Verify(){
        $Verify = new \Think\Verify();
        $Verify->entry();
    }

    //登录验证
    public function LoginVerify(){
        $mobile = I('post.mobile');
        $password = I('post.password');
        $verify   = I('post.verify');
        $verify_check = new \Think\Verify();
        /*
         * 格式：array（code=>,message=>）
         */
        if($mobile == null){
            exit(json_encode(array('code'=>101,'msg'=>'请输入用户名！')));
        }elseif($password == null){
            exit(json_encode(array('code'=>102,'msg'=>'请输入密码！')));
        }elseif(!$verify_check->check($verify) or $verify == null){
            //验证码验证失败
            exit(json_encode(array('code'=>103,'msg'=>'请输入正确的验证码！')));
        }else{
            //数据库检查逻辑结构,成功返回200
            $user = M('user');
            $ret_user = $user->where("mobile='$mobile'")->find();
            if (!$ret_user){
                $this->ajaxReturn(array('code'=>500,'msg'=>'用户不存在'));
            }

            if ($ret_user['password'] != AdminPasswordEncrypt($password,$ret_user['prefix'])){
                $this->ajaxReturn(array('code'=>500,'msg'=>'密码错误'));
            }else{
                session('uid',$ret_user['uid']);
                session('name',$ret_user['name']);

                $this->ajaxReturn(array('code'=>200));
            }

        }
    }

    //退出登录
    public function out(){
        Login_Out();
        Header("HTTP/1.1 301 Moved Permanently");
        Header("Location: ".U("Login/Index"));
    }
}